define(['app', '../../../lib/webuploader.min'], function (app, WebUploader) {
    app.controller('BookInfoModelCtrl', ['$scope', '$state', '$stateParams', '$http', '$q', 'infoService', 'categoryService', 'Upload',
        function ($scope, $state, $stateParams, $http, $q, infoService, categoryService, Upload) {

            $scope.entity = {};
            var id = $stateParams.id;
            var type = $stateParams.type;

            $scope.title = '添加';
            $scope.viewFlag = false;
            $scope.updateFlag = false;
            $scope.iconType = 1;
            $scope.editType = type;

            //获取单个阅读笔记
            $scope.getSinglebook = function (id) {
                infoService.getSinglebook(id).then(
                    function (response) {
                        //if(response.status==200)
                        //{
                        //if(response.data!=null)
                        //{
                        //    if(response.data.beginTime!=null)
                        //        response.data.beginTime=getLocalTime(response.data.beginTime);
                        //    if(response.data.endTime!=null)
                        //        response.data.endTime=getLocalTime(response.data.endTime);
                        //}
                        $scope.entity = response.data;
                        $scope.entity.boughtTime = new Date($scope.entity.boughtTime);
                        //setImage($scope.entity);
                        //}

                        //else {
                        //    alert("查询数据失败!");
                        //}
                    },
                    function (error) {
                        alert("查询数据失败！");
                    }
                );
            }
            //首次加载，获取所有图书分类信息
            $scope.getCategoryList = function () {
                categoryService.getCategoryList().then(
                    function (response) {
                        if (response != null && response.length > 0) {
                            $scope.bookClassifyData = response;
                        } else {
                            alert("查询数据失败!");
                        }
                    },
                    function (error) {
                        alert("查询数据失败！");
                    }
                );
            };
            //调用获取图书分类方法
            $scope.getCategoryList();
            if (id != null && id != '') {
                if (type == 3) {
                    $scope.title = '查看';
                    $scope.viewFlag = true;
                }
                else {
                    $scope.title = '编辑';
                    $scope.viewFlag = false;

                }
                if (type == 2) {
                    $scope.updateFlag = true;
                }
                $scope.getSinglebook(id);
            }
            $scope.save = function () {
                if ($scope.entity.name == '' || $scope.entity.name == null) {
                    alert("请填写图书名称！");
                    return;
                }
                if ($scope.entity.isbn == '' || $scope.entity.isbn == null) {
                    alert("请填写isbn！");
                    return;
                }
                if (id == null || id == '') {
                    infoService.createbook($scope.entity).then(
                        function (response) {
                            //if (response.status == 200) {
                            //    alert("保存成功！");
                            $state.go("book.infoMngr.list");
                            //}
                            //else
                            //    alert("保存失败！");
                        },
                        function (error) {
                            alert("保存失败！");
                            $state.go("book.infoMngr.list");
                        }
                    );
                }
                else {
                    infoService.updatebook($scope.entity).then(
                        function (response) {
                            //if (response.status == 200) {
                            //    alert("编辑成功！");
                            $state.go("book.infoMngr.list");
                            //}
                            //else
                            //    alert("编辑失败！");
                        },
                        function (error) {
                            alert("编辑失败！");
                            $state.go("book.infoMngr.list");
                        }
                    );
                }
            }
            $scope.cancel = function () {
                $state.go("book.infoMngr.list");
            }

            ////根据查出来的数据渲染页面
            //function setImage(o) {
            //    $scope.fileInput1 = o.icon;
            //    var lidiv = '<div id="AD_RESOURCE_DESC0" class="cp_img">' +
            //        '<img style="width:100%;height:100%;" src="' + $scope.fileInput1 + '">' +
            //        '<div class="cp_img_jian"></div></div>';
            //    $('#fileList').html(lidiv);
            //};


            $scope.uploadFiles = function (files, errFiles) {
                $scope.uploading = true;
                if (errFiles.length > 0) {
                    if (errFiles[0].$error == "maxSize") {
                        alert("上传文件的大小不能大于" + errFiles[0].$errorParam + "！");
                    }
                    else {
                        alert('有文件不符合要求，无法上传！');
                    }
                    $rootScope.loading = false;
                    return;
                }

                angular.forEach(files, function (file) {
                    //$rootScope.loading = true;
                    file.upload = Upload.upload({
                        url: 'http://47.92.95.246:8012/book/info/upload/image',
                        method: 'POST',
                        //headers: { 'Authorization': 'Bearer ' + $rootScope.identity.Token },
                        data: { 'bookid': $scope.entity.id },
                        file: file,
                    }).then(function (response) {
                        $timeout(function () {
                            $scope.model.PicturePath = response.data.Path;
                            $scope.model.PictureName = response.data.FileName;
                            $scope.model.ShowPictureName = response.data.ShowFileName;
                            $scope.model.Path = API_DURL + response.data.Path;
                            //$rootScope.loading = false;
                        });
                    }, function (response) {
                        if (response.status > 0) {
                            alert('上传失败!' + response.status + ': ' + response.data.message);
                        }
                        //$rootScope.loading = false;
                    }, function (evt) {
                        //$rootScope.loading = false;
                    });
                });
            };


        }]);
});